package tagdb.denovo;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <b>DenovoParser</b>
 * <p>
 * This simple parser reads the file contents of an output file of the PepNovo java.tagdb.algorithm.
 * </p>
 *
 * @author T.Muth
 */
public class DenovoParser {

    /**
     * Default constructor for the PepNovoParser.
     */
    public DenovoParser() {

    }

    /**
     * Reads the PepNovo output file and returns a PepNovoFile object.
     *
     * @param file
     * @return pepNovoFile PepNovoFile object
     */
    public static DenovoResult read(String file) {

        DenovoResult pepNovoFile = new DenovoResult(file);
        BufferedReader reader = null;
        DenovoEntry entry = null;
        boolean flag = false;
        List<DenovoEntry> entryList = new ArrayList<DenovoEntry>();
        List<DenovoHit> predictionList = null;
        int specNumber = 1;
        try {
            reader = new BufferedReader(new FileReader(file));
            String nextLine;

            // Iterate over all the lines of the file.
            while ((nextLine = reader.readLine()) != null) {

                // Sequence Coordinates section
                if (nextLine.startsWith(">>")) {
                    String[] temp = nextLine.split("\\s+");
                    String spectrumName = "";
                    for (int i = 3; i < temp.length; i++) {
                        if (temp[i].startsWith("(SQS")) break;
                        spectrumName += (temp[i] + " ");
                    }
                    if (entry != null) entryList.add(entry);
                    if (predictionList != null) entry.setHits(predictionList);
                    entry = new DenovoEntry();
                    //spectrumName = spectrumName.replace('\\', '/');
                    // Remove leading whitespace
                    spectrumName = spectrumName.replaceAll("^\\s+", "");
                    // Remove trailing whitespace
                    spectrumName = spectrumName.replaceAll("\\s+$", "");

                    if (spectrumName.contains("#Problem")) {
                        spectrumName = spectrumName.substring(0, spectrumName.indexOf("#"));
                    }

                    entry.setSpectrumName(spectrumName);
                    entry.setSpectrumNumber(specNumber);
                    predictionList = new ArrayList<DenovoHit>();
                    flag = true;

                    specNumber++;
                } else if (!nextLine.startsWith("#") && flag) {

                    String[] tokens = nextLine.split("\\s+");

                    DenovoHit prediction = new DenovoHit();

                    if (tokens.length > 5 && !nextLine.equals("")) {
                        prediction.setIndex(Integer.valueOf(tokens[0]));
                        prediction.setRankScore(Double.valueOf(tokens[1]));
                        prediction.setPepNovoScore(Double.valueOf(tokens[2]));
                        prediction.setNTermGap(Double.valueOf(tokens[3]));
                        prediction.setCTermGap(Double.valueOf(tokens[4]));
                        prediction.setPrecursorMh(Double.valueOf(tokens[5]));
                        prediction.setCharge(Integer.valueOf(tokens[6]));
                        if (tokens.length > 7) {
                            prediction.setSequence(tokens[7].trim());
                        }
                        prediction.setPeptide(prediction.getPeptide());
                        predictionList.add(prediction);
                    }
                }
            }
            reader.close();
            entryList.add(entry);
            if (predictionList != null) {
                entry.setHits(predictionList);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        pepNovoFile.setEntryList(entryList);
        pepNovoFile.setNumberSpectra(specNumber);
        return pepNovoFile;
    }
}

